//封装接口请求函数
import store from '@/store'
import request from './request'

//注册接口
export const registerAPI = ({username,password,repassword})=> {
    return request({
        url: '/api/reg', 
        method: 'post',
        data: {
            username,
            password,
            repassword
        }
    })
}

//登录接口
export const loginAPI = ({username,password}) => {
    return request({
        url: '/api/login',
        method: 'post',
        data: {
            username,
            password,
        }
    })
}

//获取用户信息
export const getUserInfoAPI = () => {
    return request({
        url: '/my/userinfo',
        method: 'get',
        /* 
            传参的三种方式：
                params(查询字符串query)
                data(请求体body)
                header(请求头)
        */
        // headers: {
        //     Authorization: store.state.token
        // }
    })
}

// 获取侧边栏数据
export const getMenusAPI = () => {
    return request({
        url: '/my/menus',
        // headers: {
        //     Authorization: store.state.token
        // }
    })
}

//修改用户资料
export const updateUserInfoAPI = ({ id, username, nickname, email, user_pic }) => {
    return request({
        url: '/my/userinfo',
        method: 'put',
        data: {
            id,
            username,
            nickname,
            email,
            user_pic
        }
    })
}

//更换头像
export const updateAvatarAPI = (avatar) => {
    return request({
        url: '/my/update/avatar',
        method: 'patch',
        data: {
            avatar
        }
    })
}

//更新密码
export const updatePwdAPI = ({ old_pwd, new_pwd, re_pwd}) => {
    return request({
        url: '/my/updatepwd',
        method: 'patch',
        data: {
            old_pwd,
            new_pwd,
            re_pwd
        }
    })
}

//获取文章分类
export const getArtCateListAPI = () => {
    return request({
        url: '/my/cate/list',
    })
}

//添加文章分类
export const addArtCateAPI = ({ cate_name, cate_alias}) => {
    return request({
        url: '/my/cate/add',
        method: 'post',
        data: {
            cate_name,
            cate_alias
        }
    })
}

//更新文章分类
export const updateArtCateAPI = ({ id, cate_name, cate_alias}) => {
    return request({
        url: '/my/cate/info',
        method: 'put',
        data: {
            id,
            cate_name,
            cate_alias
        }
    })
}

//文章分类删除
export const delArtCateAPI = (id) => {
    return request({
        url: '/my/cate/del',
        method: 'delete',
        params: {
            id
        }
    })
}

//发布文章
export const uploadArticleAPI = (fd) => {
    return request({
        url: '/my/article/add',
        method: 'post',
        data: fd
    })
}

//获取文章列表
export const getArticleListAPI = ({ pagenum, pagesize, cate_id, state}) => {
    return request({
        url: '/my/article/list',
        params: {
            pagenum,
            pagesize,
            cate_id,
            state
        }
    })
}

//获取文章详情
export const getArticleDetailAPI = (id) => {
    return request({
        url: '/my/article/info',
        params: {
            id
        }
    })
}

//删除文章
export const delArticleAPI = (id) => {
    return request({
        url: '/my/article/info',
        method: 'delete',
        params: {
            id
        }
    })
}